package com.example.yrdev.ordering_system.http;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 01
 *
 * @time 2016/10/19. 18:22.
 * @des 网络请求的url
 */
public class NetWorkConstant {

    public static  String BASE_URL = "http://14.146.73.97:8000/APPInterface/appService?";

    public static final String HTTP_getComTypeInfo="1005";
    public static final String HTTP_getComDetailBaseOnType="1004";
    public static final String HTTP_getOperatorInfo="1002";
    public static final String HTTP_getSystemSetting="1001";


    public static final String HTTP_uploadPurchaseRecord="2001";

    public static final String HTTP_getFixPriceTime="1008";

    public static final String HTTP_getFixPrice="1009";

    public static final String HTTP_uploadMemberCardPurchaseRecord="2002";

    public  static   String defaultIp="14.146.73.97";
    public  static   String defaultPort="8000";

    public  static  String  HTTP_getAllowCardType="1006";


   public static  void initNetwork(Context context){

     SharedPreferences preferences=context.getSharedPreferences("network", Context.MODE_PRIVATE);
     defaultIp=preferences.getString("ip",defaultIp);
     defaultPort=preferences.getString("port",defaultPort);

    reformatBaseURL();



    }
    private static void reformatBaseURL(){

        NetWorkConstant.BASE_URL="http://"+defaultIp+":"+defaultPort+"/APPInterface/appService?";


    }

   public static  void saveNetwork(Context context,String ip,String port){

        SharedPreferences preferences=context.getSharedPreferences("network", Context.MODE_PRIVATE);
         defaultIp=ip;
         defaultPort=port;

        SharedPreferences.Editor editor=preferences.edit();
        editor.putString("ip",ip);
        editor.putString("port",port);
        editor.apply();

         reformatBaseURL();


    }


    public static boolean isIPAddress(String ipaddr) {
        boolean flag = false;
        Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
        Matcher m = pattern.matcher(ipaddr);
        flag = m.matches();
        return flag;

    }


}
